package com.lanou.fooddemo.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.SubCardsBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class DetailsAdapter extends BaseAdapter {
    private LayoutInflater inflater;

    private List<DataBean> dataBeans;

    private List<SubCardsBean> subCardsBeans;

    public DetailsAdapter(Context c, List<DataBean> beans) {

        inflater = LayoutInflater.from(c);
        dataBeans = beans;



    }

    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.size();
        }

        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        subCardsBeans = new ArrayList<SubCardsBean>();
        Holder holder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.details_view, null);
            holder = new Holder();
            holder.headPortraitImageView =
                    (SmartImageView) convertView.findViewById(R.id.headPortraitImageView);
            holder.userNameText =
                    (TextView) convertView.findViewById(R.id.userNameText);
            holder.userSignatureText =
                    (TextView) convertView.findViewById(R.id.userSignatureText);
            holder.FoodImageView =
                    (SmartImageView) convertView.findViewById(R.id.FoodImageView);
            holder.detailsTextView =
                    (TextView) convertView.findViewById(R.id.detailsTextView);
            convertView.setTag(holder);

        } else {
            holder = (Holder) convertView.getTag();
        }
        if (CommonUtil.isListNotNull(dataBeans)) {
            DataBean dataBean = dataBeans.get(position);
            if (null != dataBean.getSubCards() &&
                    DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {
                subCardsBeans = dataBean.getSubCards();
            }
        }
        if (CommonUtil.isListNotNull(subCardsBeans)) {
            SubCardsBean subCardsBean = null;
            SubCardsBean subCardsBeanContent = null;
            SubCardsBean subCardsBeanImage  = null;
            SubCardsBean subCardsBeanMap = null;
            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
                if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBean = subCardsBeanTemp;
                }
                if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanContent = subCardsBeanTemp;
                }
                    if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanImage.getType())||
                            SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())){
                        subCardsBeanImage = subCardsBeanTemp;
                    }
                    if (SubCardsBean.TYPE_MAP_CARD.equals(subCardsBeanTemp.getType())){
                        subCardsBeanMap = subCardsBeanTemp;
                    }
            }
            holder.headPortraitImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
                    SmartImageView.TYPE_ROUND);
            holder.userNameText.setText(subCardsBean.getTitle());
            holder.userSignatureText.setText(subCardsBean.getTitle());
            holder.detailsTextView.setText(subCardsBeanContent.getContent());

//            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
//                if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
//                        SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
//                    subCardsBean = subCardsBeanTemp;
//                }
//            }
            String imgeUrl = null;
            if (null == subCardsBean.getPics()) {
                imgeUrl = subCardsBean.getPic().getUrl();
            } else {
                imgeUrl = subCardsBean.getPics().get(0).getUrl();
            }
            holder.FoodImageView.setImageUrl(imgeUrl);

        }
        return convertView;
    }

    private class Holder {
        public SmartImageView headPortraitImageView;
        public TextView userNameText;
        public TextView userSignatureText;
        public TextView detailsTextView;
        public SmartImageView FoodImageView;
    }

}
